package com.ruoyi.mf178.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mf178.domain.EditGoods;
import com.ruoyi.mf178.domain.Mf178Response;
import com.ruoyi.mf178.domain.UpdateGoodSupplierService;
import com.ruoyi.mf178.service.Mf178Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mf178.mapper.Mf178SupplyGoodMapper;
import com.ruoyi.mf178.domain.Mf178SupplyGood;
import com.ruoyi.mf178.service.IMf178SupplyGoodService;

/**
 * mf178Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-14
 */
@Slf4j
@Service
public class Mf178SupplyGoodServiceImpl implements IMf178SupplyGoodService {
    @Autowired
    private Mf178SupplyGoodMapper mf178SupplyGoodMapper;
    @Autowired
    private CouponTradeMcd couponTradeMcd;
    @Autowired
    Mf178Api mf178Api;

    /**
     * 查询mf178
     *
     * @param goodsId mf178主键
     * @return mf178
     */
    @Override
    public Mf178SupplyGood selectMf178SupplyGoodByGoodsId(Long goodsId) {
        return mf178SupplyGoodMapper.selectMf178SupplyGoodByGoodsId(goodsId);
    }

    /**
     * 查询mf178列表
     *
     * @param mf178SupplyGood mf178
     * @return mf178
     */
    @Override
    public List<Mf178SupplyGood> selectMf178SupplyGoodList(Mf178SupplyGood mf178SupplyGood) {
        return mf178SupplyGoodMapper.selectMf178SupplyGoodList(mf178SupplyGood);
    }

    /**
     * 新增mf178
     *
     * @param mf178SupplyGood mf178
     * @return 结果
     */
    @Override
    public int insertMf178SupplyGood(Mf178SupplyGood mf178SupplyGood) {
        mf178SupplyGood.setCreateTime(DateUtils.getNowDate());
        return mf178SupplyGoodMapper.insertMf178SupplyGood(mf178SupplyGood);
    }

    /**
     * 修改mf178
     *
     * @param mf178SupplyGood mf178
     * @return 结果
     */
    @Override
    public int updateMf178SupplyGood(Mf178SupplyGood mf178SupplyGood) {
        mf178SupplyGood.setUpdateTime(DateUtils.getNowDate());
        return mf178SupplyGoodMapper.updateMf178SupplyGood(mf178SupplyGood);
    }

    /**
     * 批量删除mf178
     *
     * @param goodsIds 需要删除的mf178主键
     * @return 结果
     */
    @Override
    public int deleteMf178SupplyGoodByGoodsIds(Long[] goodsIds) {
        return mf178SupplyGoodMapper.deleteMf178SupplyGoodByGoodsIds(goodsIds);
    }

    /**
     * 删除mf178信息
     *
     * @param goodsId mf178主键
     * @return 结果
     */
    @Override
    public int deleteMf178SupplyGoodByGoodsId(Long goodsId) {
        return mf178SupplyGoodMapper.deleteMf178SupplyGoodByGoodsId(goodsId);
    }

    @Override
    public int updateGoodSupplierService(UpdateGoodSupplierService updateGood) {
        return mf178SupplyGoodMapper.updateGoodSupplierService(updateGood);
    }

    public void syncGoods(Mf178Response resultJson) {
        JSONObject data = (JSONObject) resultJson.getData();
        JSONArray goodsInfo = data.getJSONArray("goods_info");

        for (int i = 0; i < goodsInfo.size(); i++) {
            JSONObject good = goodsInfo.getJSONObject(i);
            Long goodsId = good.getLong("goods_id");
            Mf178SupplyGood supplyGood = selectMf178SupplyGoodByGoodsId(goodsId);
            boolean insert = supplyGood == null;
            if (insert) {
                supplyGood = new Mf178SupplyGood();
                supplyGood.setGoodsId(goodsId);
            }
            supplyGood.setGoodsSku(good.getString("goods_sku"));
            supplyGood.setGoodsName(good.getString("goods_name"));
            supplyGood.setbName(good.getString("b_name"));
            supplyGood.setVenderName(good.getString("vender_name"));
            supplyGood.setSpecName(good.getString("spec_name"));
            supplyGood.setSpecValueIds(good.getString("spec_value_ids"));
            if (good.getJSONObject("order_limit_config") != null) {
                supplyGood.setExternalCode(good.getJSONObject("order_limit_config").getString("external_code"));
            }
            if (good.getJSONObject("user_quote_stock_info") != null) {
                supplyGood.setUserQuotePayment(new BigDecimal(good.getJSONObject("user_quote_stock_info").getString("user_quote_payment")));
            }
            supplyGood.setSupplyStatus(good.getInteger("supply_status"));
            supplyGood.setSupplierService("mcd_xhl");
            supplyGood.setOfficialProductName(supplyGood.getSpecName());
//            supplyGood.setOfficialPrice();

            if (insert) {
                insertMf178SupplyGood(supplyGood);
            } else {
                updatePrice(supplyGood);
                updateMf178SupplyGood(supplyGood);
            }
        }
    }

    public void updatePrice(Mf178SupplyGood supplyGood) {
        // 修改信息这边调用查询 获取价格 查看是否有变更 有的话 调用修改接口修改价格
        if (supplyGood.getExternalCode() == null || !StringUtils.isNotBlank(supplyGood.getExternalCode())) {
            return;
        }
        String merchantPackageInfo = couponTradeMcd.merchantPackageInfo(supplyGood.getExternalCode());
        if (!StringUtils.isNotBlank(merchantPackageInfo)) {
            return;
        }
        JSONObject infoJSON = JSONObject.parseObject(merchantPackageInfo);
        Integer code = infoJSON.getInteger("code");
        if (code == null || !code.equals(200)) {
            return;
        }
        BigDecimal price = infoJSON.getJSONObject("data").getBigDecimal("merchantSalePrice");
        String saleStatus = infoJSON.getJSONObject("data").getString("saleStatus");
        if (supplyGood.getUserQuotePayment().compareTo(price) == 0 && saleStatus.equals(supplyGood.getSupplyStatus().toString())) {
            return;
        }
        Integer status = 2;
        if (saleStatus.equals("1")) {
            status = 1;
        } else if (saleStatus.equals("0")) {
            status = 2;
        }
        supplyGood.setUserQuotePayment(price);
        //价格不同则调用修改价格接口
        List<EditGoods> editGoodsList = new ArrayList<>();
        EditGoods goods = new EditGoods();
        goods.setGoods_id(supplyGood.getGoodsId().intValue());
        goods.setStatus(status); // 固定下架
        goods.setUser_quote_payment(price);
        editGoodsList.add(goods);
        mf178Api.editSupplyGoodManageStock(editGoodsList);
        log.info("-----价格变更，goodsId：" + supplyGood.getGoodsId() + "修改价格");
    }
}
